// 检查窗口大小
if (window.innerWidth < 1000) {
  console.error(
    "当前宽度" +
      window.innerWidth +
      "，窗口太小，会导致音频无法自动播放，请把浏览器窗口拉大，宽度至少1000"
  );
} else {
  let iframe = document.createElement("iframe");
  iframe.style.width = "95%";
  iframe.style.height = "80%";
  iframe.style.position = "fixed";
  iframe.style.left = 0;
  iframe.style.top = 0;
  iframe.style.right = 0;
  iframe.style.bottom = 0;
  iframe.style.margin = "0 auto";
  iframe.style.border = "4px solid red";
  iframe.style.zIndex = 1000;
  iframe.src = document.location.href;
  document.body.appendChild(iframe);

  // 模拟点击音频开始按钮
  function clickAudioStart() {
    let eAudioStart = iframe.contentWindow.document.getElementsByClassName(
      "AudioPlayerPC_btnPlay_3yo10"
    )[0];
    eAudioStart.dispatchEvent(
      new MouseEvent("click", {
        bubbles: true,
        cancelable: true,
        view: iframe.contentWindow,
      })
    );
  }

  // 模拟点击下一章按钮
  function clickNextIcon() {
    let eSideMenuBtns = Array.from(
      iframe.contentWindow.document.getElementsByClassName(
        "RightSideMenuPC_icon-base_3CG6j"
      )
    );
    for (let i = 0, len = eSideMenuBtns.length; i < len; i++) {
      if (eSideMenuBtns[i].innerHTML.indexOf("下一篇") >= 0) {
        eSideMenuBtns[i].dispatchEvent(
          new MouseEvent("click", {
            bubbles: true,
            cancelable: true,
            view: iframe.contentWindow,
          })
        );
        break;
      }
    }
  }

  // 格式化时间
  function formatTime(timestamp) {
    let date = timestamp ? new Date(timestamp) : new Date();
    let h = date.getHours().toString().padStart(2, "0");
    let m = date.getMinutes().toString().padStart(2, "0");
    let s = date.getSeconds().toString().padStart(2, "0");
    return `#${h}:${m}:${s}# `;
  }

  // iframe加载完成回调
  function iframeLoaded() {
    clickAudioStart();
    let eAudioInfo = iframe.contentWindow.document.getElementsByClassName(
      "AudioPlayerPC_audioInfo_PRzuh"
    )[0];
    let durationStr = "";
    if (eAudioInfo.children && eAudioInfo.children.length > 0) {
      for (let i = 0, len = eAudioInfo.children.length; i < len; i++) {
        if (eAudioInfo.children[i].innerHTML.startsWith("时长：")) {
          durationStr = eAudioInfo.children[i].innerHTML.substring(3).trim();
          break;
        }
      }
    }
    let ary = durationStr.split(":");
    let seconds = durationStr ? parseInt(ary[0]) * 60 + parseInt(ary[1]) : 600;
    let waitMillis = (seconds + 10) * 1000;
    let waitUntil = formatTime(new Date().getTime() + waitMillis);
    console.log(
      formatTime(null) + "duration is " + seconds + "s, wait until " + waitUntil
    );
    // 等待视频时长+额外10s后，模拟点击下一章
    setTimeout(() => {
      console.log(formatTime(null) + "next chapter, wait for 10s");
      clickNextIcon();
      setTimeout(iframeLoaded, 10000);
    }, waitMillis);
  }

  iframe.addEventListener(
    "load",
    () => {
      console.log(formatTime(null) + "iframe loaded, wait for 10s");
      setTimeout(iframeLoaded, 10000); // 等10s，让子页面加载完并开始播放
    },
    true
  );
}
